package fgame.controller.state
{
	import fgame.GameState;
	import fgame.controller.Controller;
	import fgame.controller.state.State;
	import fgame.unit.Actor3D;
	
	import flare.basic.Scene3D;
	import flare.collisions.CollisionInfo;
	import flare.collisions.RayCollision;
	import flare.collisions.SphereCollision;
	import flare.core.Pivot3D;
	import flare.system.Input3D;
	import flare.utils.Pivot3DUtils;
	
	import flash.events.Event;
	import flash.geom.Vector3D;
	
	import ricky.utils.TimeServer;
	
	public class ControllState extends State
	{
		public function ControllState(controller:Controller=null)
		{
			super(controller);
			_types=3;
			currState=new Object();
			lastState=new Object();
		}
		
		override public function begin():void
		{
			super.begin();

			currState=getKeyState();
			lastState=currState;
			MsgHelper.sendActMsg(controller.target.name,this.types,currState,11);

		}
		
		
		override public function action(time:Number):void
		{
			
			if(checkStateChange())//如果状态改变
			{
				MsgHelper.sendActMsg(controller.target.name,this.types,currState,11);
				
				if(currState[0] == false &&
					currState[1] == false &&
					currState[2] == false &&
					currState[3] == false)
				{
					this.complete();
//					MsgHelper.synchrostep();
				}
			}
			
//			if ( Input3D.keyDown( Input3D.UP ) ) speed -= 0.2;
//			if ( Input3D.keyDown( Input3D.DOWN ) ) speed += 0.2;
//			
//			if ( Input3D.keyDown( Input3D.LEFT ) ) direction += 6;
//			if ( Input3D.keyDown( Input3D.RIGHT ) )  direction -= 6;
//			
//			MsgHelper.sendActMsg("act",controller.target.name,this.types,[x,y,z,roY,info.normal.x,info.normal.y,info.normal.z]);

//			speed *= 0.9;
//			direction *= 0.9;
//			
//			var roy:Number= direction * speed / 20;
//			
//			_targetPivot3D.translateZ( speed );
//			
//			var from:Vector3D = _targetPivot3D.localToGlobal( new Vector3D( 0, 50, 0 ) );
//			var dir:Vector3D = _targetPivot3D.getDown();
//			
//			var info:CollisionInfo;
//			if(controller.rayCollisions.test(from,dir))
//			{
//				info = controller.rayCollisions.data[0];
				
//				_targetPivot3D.setPosition( info.point.x, info.point.y, info.point.z );
				
//				_targetPivot3D.setNormalOrientation( info.normal, 0.05 );
				
//			}
			
//			controller.target.sphereCollision.slider();
//			
//			if(info)
//			{
//				
//				var x:Number=info.point.x;
//				var y:Number=info.point.y;
//				var z:Number=info.point.z;
//				var roY:Number=roy;
//				MsgHelper.sendActMsg("act",controller.target.name,this.types,[x,y,z,roY,info.normal.x,info.normal.y,info.normal.z]);
//			}
				
		
		}
		
		override public function end():void
		{
//			GameState.inst.sceneManager.removeCollisionWithObstacle(controller.target.sphereCollision);
//			
//			var actor3d:Actor3D=controller.target;
//			actor3d.playAnimAct("wait");
		}
		
		protected function checkStateChange():Boolean
		{
			currState=getKeyState();
			if(currState["a"] == lastState["a"] &&
				currState["b"] == lastState["b"] &&
				currState["c"] == lastState["c"] &&
				currState["d"] == lastState["d"])
				return false;
			return true;
		}
	
		protected function getKeyState():Object
		{
			currState["a"]=Input3D.keyDown( Input3D.UP );
			currState["b"]=Input3D.keyDown( Input3D.DOWN );
			currState["c"]=Input3D.keyDown( Input3D.LEFT );
			currState["d"]=Input3D.keyDown( Input3D.RIGHT );
			currState
			return currState;
		}
		
	
		
		private var lastState:Object;
		private var currState:Object;
		
		private var speed:Number=0,direction:Number=0;
	}
}